package com.app.controller.importExcel;

import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.app.model.*;
import com.app.service.*;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.List;

@Slf4j
@RestController
@Controller
@RequestMapping("/api/import")
public class ImportExcel {
    @Autowired
    private ComputerService computerService;
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    AttachmentService attachmentService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    private TaskService taskService;

    @PostMapping("/computerExcel")
    public Result<Object> computerExcel(MultipartFile file, HttpServletRequest request) throws Exception {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        reader.addHeaderAlias("序号", "number");
        reader.addHeaderAlias("单位", "departmentText");
        reader.addHeaderAlias("责任人", "personName");
        reader.addHeaderAlias("计算机编号", "name");
        reader.addHeaderAlias("内置硬盘序列号", "diskNumber");
        reader.addHeaderAlias("品牌型号", "brandModel");
        reader.addHeaderAlias("购置/启用时间", "activationTime");
        reader.read(10);
        //reader.read(标头行数，开始行数，对象)
        List<ComputerModel> computerModelList = reader.read(1, 2, ComputerModel.class);
        for (int i = 0; i < computerModelList.size(); i++) {
            ComputerModel computerModel = computerModelList.get(i);
            DepartmentModel departmentModel = departmentService.getdepByName(computerModel.getDepartmentText());
            if (departmentModel != null) {
                computerModel.setDepartmentId(departmentModel.getId());
//                int row = i + 3;
//                return Result.error(500, "第" + row + "行单位不存在");
            }

            computerModel.setCreateTime(CommonHelp.getNowTimestamp());
            computerModel.setType(1);
            computerService.insert(computerModel);
        }
        return Result.success("");
    }

    @PostMapping("/researchComputerExcel")
    public Result<Object> researchComputerExcel(MultipartFile file, HttpServletRequest request) throws Exception {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        reader.addHeaderAlias("序号", "number");
        reader.addHeaderAlias("单位", "departmentText");
        reader.addHeaderAlias("责任人", "personName");
        reader.addHeaderAlias("项目名称", "projectName");
        reader.addHeaderAlias("MAC地址", "macAddress");
        reader.addHeaderAlias("计算机编号", "name");
        reader.addHeaderAlias("内置硬盘序列号", "diskNumber");
        reader.addHeaderAlias("品牌型号", "brandModel");
        reader.addHeaderAlias("购置/启用时间", "activationTime");
        //reader.read(标头行数，开始行数，对象)
        List<ComputerModel> computerModelList = reader.read(1, 2, ComputerModel.class);
        for (int i = 0; i < computerModelList.size(); i++) {
            ComputerModel computerModel = computerModelList.get(i);
            DepartmentModel departmentModel = departmentService.getdepByName(computerModel.getDepartmentText());
            if (departmentModel != null) {
                computerModel.setDepartmentId(departmentModel.getId());
//                int row = i + 3;
//                return Result.error(500, "第" + row + "行单位不存在");
            }
            computerModel.setCreateTime(CommonHelp.getNowTimestamp());
            computerModel.setType(2);
            computerService.insert(computerModel);
        }
        return Result.success("");
    }

    @PostMapping("/standComputerExcel")
    public Result<Object> standComputerExcel(MultipartFile file, HttpServletRequest request) throws Exception {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        reader.addHeaderAlias("序号", "number");
        reader.addHeaderAlias("单位", "departmentText");
        reader.addHeaderAlias("责任人", "personName");
        reader.addHeaderAlias("业务内容", "businessContent");
        reader.addHeaderAlias("计算机编号", "name");
        reader.addHeaderAlias("内置硬盘序列号", "diskNumber");
        reader.addHeaderAlias("品牌型号", "brandModel");
        reader.addHeaderAlias("购置/启用时间", "activationTime");
        //reader.read(标头行数，开始行数，对象)
        List<ComputerModel> computerModelList = reader.read(1, 2, ComputerModel.class);
        for (int i = 0; i < computerModelList.size(); i++) {
            ComputerModel computerModel = computerModelList.get(i);
            DepartmentModel departmentModel = departmentService.getdepByName(computerModel.getDepartmentText());
            if (departmentModel != null) {
                computerModel.setDepartmentId(departmentModel.getId());
//                int row = i + 3;
//                return Result.error(500, "第" + row + "行单位不存在");
            }
            computerModel.setCreateTime(CommonHelp.getNowTimestamp());
            computerModel.setType(3);
            computerService.insert(computerModel);
        }
        return Result.success("");
    }

    @PostMapping("/hardDiskComputerExcel")
    public Result<Object> hardDiskComputerExcel(MultipartFile file, HttpServletRequest request) throws Exception {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        reader.addHeaderAlias("序号", "number");
        reader.addHeaderAlias("单位", "departmentText");
        reader.addHeaderAlias("责任人", "personName");
        reader.addHeaderAlias("设备编号", "equipmentNumber");
        reader.addHeaderAlias("硬盘序列号", "diskNumber");
        reader.addHeaderAlias("品牌型号", "brandModel");
        reader.addHeaderAlias("购置/启用时间", "activationTime");
        reader.addHeaderAlias("内存卡编号", "memoryCard");
        reader.addHeaderAlias("备注", "remark");
        //reader.read(标头行数，开始行数，对象)
        List<ComputerModel> computerModelList = reader.read(1, 2, ComputerModel.class);
        for (int i = 0; i < computerModelList.size(); i++) {
            ComputerModel computerModel = computerModelList.get(i);
            DepartmentModel departmentModel = departmentService.getdepByName(computerModel.getDepartmentText());
            if (departmentModel != null) {
                computerModel.setDepartmentId(departmentModel.getId());
//                int row = i + 3;
//                return Result.error(500, "第" + row + "行单位不存在");
            }
            computerModel.setCreateTime(CommonHelp.getNowTimestamp());
            computerModel.setType(4);
            computerService.insert(computerModel);
        }
        return Result.success("");
    }

    @PostMapping("/elseComputerExcel")
    public Result<Object> elseComputerExcel(MultipartFile file, HttpServletRequest request) throws Exception {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        reader.addHeaderAlias("序号", "number");
        reader.addHeaderAlias("单位", "departmentText");
        reader.addHeaderAlias("责任人", "personName");
        reader.addHeaderAlias("设备编号", "equipmentNumber");
        reader.addHeaderAlias("设备类型", "equipmentType");
        reader.addHeaderAlias("品牌型号", "brandModel");
        reader.addHeaderAlias("购置/启用时间", "activationTime");
        reader.addHeaderAlias("备注", "remark");
        //reader.read(标头行数，开始行数，对象)
        List<ComputerModel> computerModelList = reader.read(1, 2, ComputerModel.class);
        for (int i = 0; i < computerModelList.size(); i++) {
            ComputerModel computerModel = computerModelList.get(i);
            DepartmentModel departmentModel = departmentService.getdepByName(computerModel.getDepartmentText());
            if (departmentModel != null) {
                computerModel.setDepartmentId(departmentModel.getId());
//                int row = i + 3;
//                return Result.error(500, "第" + row + "行单位不存在");
            }

            computerModel.setCreateTime(CommonHelp.getNowTimestamp());
            computerModel.setType(5);
            computerService.insert(computerModel);
        }
        return Result.success("");
    }

    @PostMapping("/OfficeComputerExcel")
    public Result<Object> OfficeComputerExcel(MultipartFile file, HttpServletRequest request) throws Exception {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        reader.addHeaderAlias("序号", "number");
        reader.addHeaderAlias("单位", "departmentText");
        reader.addHeaderAlias("责任人", "personName");
        reader.addHeaderAlias("计算机编号", "equipmentNumber");
        reader.addHeaderAlias("内置硬盘序列号", "equipmentType");
        reader.addHeaderAlias("品牌型号", "brandModel");
        reader.addHeaderAlias("购置/启用时间", "activationTime");
        reader.addHeaderAlias("Ip地址", "ipAddress");
        reader.addHeaderAlias("Mac地址", "macAddress");
        reader.addHeaderAlias("备注", "remark");
        //reader.read(标头行数，开始行数，对象)
        List<ComputerModel> computerModelList = reader.read(1, 2, ComputerModel.class);
        for (int i = 0; i < computerModelList.size(); i++) {
            ComputerModel computerModel = computerModelList.get(i);
            DepartmentModel departmentModel = departmentService.getdepByName(computerModel.getDepartmentText());
            if (departmentModel != null) {
                computerModel.setDepartmentId(departmentModel.getId());
//                int row = i + 3;
//                return Result.error(500, "第" + row + "行单位不存在");
            }
            computerModel.setCreateTime(CommonHelp.getNowTimestamp());
            computerModel.setType(6);
            computerService.insert(computerModel);
        }
        return Result.success("");
    }

    @PostMapping("/importTaskExcel")
    public Result<Object> importTaskExcel(MultipartFile file, HttpServletRequest request) throws Exception {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        reader.addHeaderAlias("序号", "number");
        reader.addHeaderAlias("名称", "name");
        reader.addHeaderAlias("所属单位", "departmentText");
        reader.addHeaderAlias("提醒时间（天）", "warnTime");
        reader.addHeaderAlias("进度条", "progressRequirements");
        reader.addHeaderAlias("要求时间", "taskTime");
        reader.addHeaderAlias("监督人", "supervisor");
        reader.addHeaderAlias("责任人", "personName");
        reader.addHeaderAlias("任务进度", "progress");
        //reader.read(标头行数，开始行数，对象)
        List<TaskModel> taskModelList = reader.read(1, 2, TaskModel.class);
        for (TaskModel taskModel : taskModelList) {
            if (StrUtil.isNotBlank(taskModel.getDepartmentText())) {
                String[] strings = taskModel.getDepartmentText().split(",");
                StringBuilder stringBuilder = new StringBuilder();
                for (String string : strings) {
                    DepartmentModel departmentModel = departmentService.getdepByName(string);
                    if (departmentModel != null) {
                        stringBuilder.append(departmentModel.getId()).append(",");
                    }
                }
                stringBuilder.deleteCharAt(stringBuilder.length() - 1);
                taskModel.setDepartmentId(stringBuilder.toString());
            }
            taskService.insert(taskModel);
        }
        return Result.success("");
    }
}
